<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class StatisticsDayEnergy extends Model
{
    //

    use SoftDeletes;


    public static function getOneDayBySiteId($site_id, $date)
    {
        $sde = StatisticsDayEnergy::where('site_id', $site_id)
            ->where('date', $date)->first();
        return $sde;
    }

    public static function getOneDayByUserId($user_id, $date)
    {
        $sdes = StatisticsDayEnergy::where('user_id', $user_id)
            ->where('date', $date)->get();
        return $sdes;
    }

    public static function getSomeByUserId($user_id, $begin, $end)
    {
        $sdes = StatisticsDayEnergy::where('user_id', $user_id)
            ->whereBetween('date', [$begin, $end])->get();
        return $sdes;
    }

    public static function getSomeBySiteId($site_id, $begin, $end)
    {
        $sdes = StatisticsDayEnergy::where('site_id', $site_id)
            ->whereBetween('date', [$begin, $end])->get();
        return $sdes;
    }

    public static function getOneMonthByUserId($user_id, $m)
    {
        $begin = $m . '-01';
        $end = date('Y-m-d', strtotime('+1 month', strtotime($begin)));
        $sdes = StatisticsDayEnergy::where('user_id', $user_id)
            ->whereBetween('date', [$begin, $end])->get();
        $total = 0;
        foreach ($sdes as $sde) {
            $total = $total + $sde->user_value;
        }
        return $total;
    }

    public static function getOneMonthBySiteId($site_id, $m)
    {
        $begin = $m . '-01';
        $end = date('Y-m-d', strtotime('+1 month', strtotime($begin)));
        $sdes = StatisticsDayEnergy::where('site_id', $site_id)
            ->whereBetween('date', [$begin, $end])->get();
        $total = 0;
        foreach ($sdes as $sde) {
            $total = $total + $sde->site_value;
        }
        return $total;
    }
}
